package com.yunhe.policyfulfillment.policyful.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.common.util.StringUtil;
import com.yunhe.policyfulfillment.authority.domain.PolicyOrg;
import com.yunhe.policyfulfillment.authority.repository.IPolicyOrgRepository;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.Appeal;
import com.yunhe.policyfulfillment.policyful.domain.AppealSatisfaction;
import com.yunhe.policyfulfillment.policyful.model.DateCondition;
import com.yunhe.policyfulfillment.policyful.model.IndexSatisfactionStatistics;
import com.yunhe.policyfulfillment.policyful.model.SatisfactionStatistics;
import com.yunhe.policyfulfillment.policyful.repository.AppealSatisfactionRepository;
import com.yunhe.policyfulfillment.policyful.service.AppealSatisfactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: AppealSatisfactionServiceImpl
 * @Description:
 * @author: GongDaCheng
 * @Date: 13:43 2021/10/8
 */
@Service
public class AppealSatisfactionServiceImpl implements AppealSatisfactionService {

    @Autowired
    private AppealSatisfactionRepository appealSatisfactionRepository;

    @Autowired
    private IPolicyOrgRepository iPolicyOrgRepository;

    @Override
    public int createAppealSatisfaction(AppealSatisfaction appealSatisfaction) throws ArgumentErrorException {
        AppealSatisfaction appealSatisfactionByAppealId = appealSatisfactionRepository.searchAppealSatisfactionByAppealId(appealSatisfaction.getAppealId());
        if (appealSatisfactionByAppealId != null){
            throw new ArgumentErrorException("该诉求已进行过满意度评价，请勿重复评价!");
        }
        appealSatisfaction.setCreateTime(new Date());
        return appealSatisfactionRepository.createAppealSatisfactionSelective(appealSatisfaction);
    }

    @Override
    public int deleteAppealSatisfaction(long id) {
        return appealSatisfactionRepository.deleteAppealSatisfactionByPrimaryKey(id);
    }

    @Override
    public int updateAppealSatisfaction(AppealSatisfaction appealSatisfaction) {
        return appealSatisfactionRepository.updateAppealSatisfactionByPrimaryKeySelective(appealSatisfaction);
    }

    @Override
    public AppealSatisfaction findAppealSatisfactionById(long id) {
        return appealSatisfactionRepository.searchAppealSatisfactionByPrimaryKey(id);
    }

    @Override
    public IndexSatisfactionStatistics indexAppealSatisfactionStatistics() {
        IndexSatisfactionStatistics indexSatisfactionStatistics = new IndexSatisfactionStatistics();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);

        int satisfiedCount = appealSatisfactionRepository.getCountBySatisfaction(PolicyFulfillmentConstant.Appeal_Satisfaction.Satisfied.value(), year);
        int quiteSatisfiedCount = appealSatisfactionRepository.getCountBySatisfaction(PolicyFulfillmentConstant.Appeal_Satisfaction.QuiteSatisfied.value(), year);
        int dissatisfiedCount = appealSatisfactionRepository.getCountBySatisfaction(PolicyFulfillmentConstant.Appeal_Satisfaction.Dissatisfied.value(), year);

        indexSatisfactionStatistics.setSatisfiedCount(satisfiedCount);
        indexSatisfactionStatistics.setQuiteSatisfiedCount(quiteSatisfiedCount);
        indexSatisfactionStatistics.setDissatisfiedCount(dissatisfiedCount);

        return indexSatisfactionStatistics;
    }

    @Override
    public PageResult satisfactionStatistics(DateCondition dateCondition) {
        PageResult result = null;
        if (StringUtil.isEmpty(dateCondition.getStartTime()) && StringUtil.isEmpty(dateCondition.getEndTime())){
            //startTime endTime为空，默认统计最近三个月的数据
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.MONTH, -3);
            int oldYear = cal.get(Calendar.YEAR);
            int oldMonth = cal.get(Calendar.MONTH);
            String firstDayOfThreeMonthAgo = DateTimeUtils.getFirstDayOfMonth(oldYear, oldMonth);
            firstDayOfThreeMonthAgo += " 00:00:00";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = sdf.format(new Date());
            dateCondition.setStartTime(firstDayOfThreeMonthAgo);
            dateCondition.setEndTime(now);
        }
        PageHelper.startPage(dateCondition.getPage(), dateCondition.getSize());
        List<SatisfactionStatistics> satisfactionStatistics = appealSatisfactionRepository.satisfactionStatistics(dateCondition);
        for (SatisfactionStatistics satisfactionStatistic : satisfactionStatistics) {
            if (satisfactionStatistic.getHandleOrg() != null){
                PolicyOrg policyOrg = iPolicyOrgRepository.searchPolicyOrgByPrimaryKey(satisfactionStatistic.getHandleOrg().intValue());
                if (policyOrg != null){
                    satisfactionStatistic.setHandleOrgTitle(policyOrg.getOrgName());
                }
            }
            if (satisfactionStatistic.getSatisfactionEvaluation() != null){
                String satisfactionEvaluationTitle = PolicyFulfillmentConstant.Appeal_Satisfaction.getTitleByValue(satisfactionStatistic.getSatisfactionEvaluation());
                satisfactionStatistic.setSatisfactionEvaluationTitle(satisfactionEvaluationTitle);
            }
        }
        PageInfo<SatisfactionStatistics> pageInfo = new PageInfo<>(satisfactionStatistics);
        result = new PageResult(dateCondition.getPage(), dateCondition.getSize(), (int)pageInfo.getTotal(), satisfactionStatistics);
        return result;
    }

}
